local skel = fk.CreateSkill {
  name = "lb_zero__ewang",
  tags = { Skill.Switch },
}

Fk:loadTranslationTable {
  ["lb_zero__ewang"] = "遏妄",
  [":lb_zero__ewang"] = "<b>转换技</b>，阳：每回合开始时，你可以使用一张牌，目标角色本回合不能使用与之花色相同的牌；阴：出牌阶段，你可以将手牌摸至体力上限，本回合不能使用与上张牌花色相同的牌。",

  [":lb_zero__ewang_yang"] = "<b>转换技</b>，<font color=\"#E0DB2F\">阳：每回合开始时，你可以使用一张牌，目标角色本回合不能使用与之花色相同的牌；</font>阴：出牌阶段，你可以将手牌摸至体力上限，本回合不能使用与上张牌花色相同的牌。",
  [":lb_zero__ewang_yin"] = "<b>转换技</b>，阳：每回合开始时，你可以使用一张牌，目标角色本回合不能使用与之花色相同的牌；<font color=\"#E0DB2F\">阴：出牌阶段，你可以将手牌摸至体力上限，本回合不能使用与上张牌花色相同的牌。</font>",


  ["#lb_zero__ewang-draw"] = "遏妄：你可以摸 %arg 张牌",
  ["#lb_zero__ewang-use"] = "遏妄：你可以使用一张牌，目标角色本回合不能使用与之花色相同的牌",

  ["@[lb_zero__ewang]lb_zero__ewang-turn"] = "遏妄",

  ["$lb_zero__ewang1"] = "来尝尝云岿山厨子的独门术法！",
  ["$lb_zero__ewang2"] = "破-棍！",
  ["$lb_zero__ewang3"] = "神-阙！",
  ["$lb_zero__ewang4"] = "气-绝！",
}

Fk:addQmlMark {
  name = skel.name,
  how_to_show = function(name, value, player)
    if type(value) ~= "table" then return "" end
    local mark = ""
    if value.suit and value.tos then
      if #value.tos > 0 then
        mark = tostring(#value.tos)
      end
      mark = mark .. Fk:translate("yang") .. Fk:translate(Card.getSuitString({ suit = value.suit }, true))
    end
    if value.notsuit then
      mark = mark .. Fk:translate("yin") .. Fk:translate(Card.getSuitString({ suit = value.notsuit }, true))
    end
    return mark
  end,
  qml_data = function(name, value, player)
    if type(value) ~= "table" then return "" end
    local ids = {}
    for key, val in pairs(value) do
      if key == "tos" and type(val) == "table" then
        ids = val
      end
    end
    return ids
  end,
  qml_path = "packages/utility/qml/PlayerBox"
}

skel:addEffect("active", {
  audio_index = {3, 4},
  anim_type = "switch",
  prompt = function(self, player, selected_cards, selected_targets)
    return "#lb_zero__ewang-draw:::" .. player.maxHp - player:getHandcardNum()
  end,
  can_use = function(self, player)
    return player:getSwitchSkillState(skel.name) == fk.SwitchYin and player.maxHp > player:getHandcardNum()
  end,
  target_num = 0,
  card_num = 0,
  on_use = function(self, room, skillUseEvent)
    local player = skillUseEvent.from
    player:drawCards(player.maxHp - player:getHandcardNum(), skel.name)
    if player.dead then return end
    local lastUseEvent = room.logic:getEventsByRule(GameEvent.UseCard, 1, function(e)
      return true
    end, 0)[1]
    if lastUseEvent == nil then return false end
    local last_use = lastUseEvent.data
    local suit = last_use.card.suit
    if suit ~= Card.NoSuit then
      local list = player:getTableMark("@[lb_zero__ewang]lb_zero__ewang-turn")
      list.notsuit = suit
      room:setPlayerMark(player, "@[lb_zero__ewang]lb_zero__ewang-turn", list)
    end
  end,
})

skel:addEffect(fk.TurnStart, {
  audio_index = {1, 2},
  anim_type = "switch",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and (#player:getHandlyIds() + #player:getCardIds("e")) > 0
        and player:getSwitchSkillState(skel.name) == fk.SwitchYang
  end,
  on_cost = function(self, event, target, player, data)
    local use = player.room:askToPlayCard(player, {
      pattern = ".",
      skill_name = skel.name,
      prompt = "#lb_zero__ewang-use",
      skip = true,
    })
    if use then
      event:setCostData(self, { use = use })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local use = event:getCostData(self).use ---@type UseCardDataSpec
    local suit = use.card.suit
    if suit ~= Card.NoSuit then
      room:setPlayerMark(player, "@[lb_zero__ewang]lb_zero__ewang-turn", {
        suit = suit,
        tos = table.map(use.tos, Util.IdMapper)
      })
      use.extra_data = use.extra_data or {}
      use.extra_data.lb_zero__ewang = player
    end
    player.room:useCard(event:getCostData(self).use)
  end,
})

skel:addEffect("prohibit", {
  prohibit_use = function(self, player, card)
    for _, to in ipairs(Fk:currentRoom().alive_players) do
      if to:hasSkill(skel.name) then
        local list = to:getTableMark("@[lb_zero__ewang]lb_zero__ewang-turn")
        return (list.suit == card.suit and table.contains(list.tos or {}, player.id)) or
            (list.notsuit == card.suit and to == player)
      end
    end
  end,
})

local ewang_spec = {
  can_refresh = function(self, event, target, player, data)
    return target == player and data.extra_data and data.extra_data.lb_zero__ewang == player
  end,
  on_refresh = function(self, event, target, player, data)
    local list = player:getTableMark("@[lb_zero__ewang]lb_zero__ewang-turn")
    list.tos = list.tos or {}
    local tos = {}
    if (event == fk.CardUsing or event == fk.AfterCardUseDeclared) then
      tos = table.simpleClone(data.tos)
    else
      tos = { data.to }
    end
    if #tos > 0 then
      for _, to in ipairs(tos) do
        table.insertIfNeed(list.tos, to.id)
      end
      player.room:setPlayerMark(player, "@[lb_zero__ewang]lb_zero__ewang-turn", list)
    end
  end
}

skel:addEffect(fk.AfterCardUseDeclared, ewang_spec)
skel:addEffect(fk.CardUsing, ewang_spec)
skel:addEffect(fk.TargetSpecifying, ewang_spec)
skel:addEffect(fk.TargetSpecified, ewang_spec)

return skel
